package hxy.grandfather.live.launcher;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Binder;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 开机启动指定应用的核心程序
 */
public class AppListService extends Service {
    Logger log = LoggerFactory.getLogger(AppListService.class);

    private String TAG = "live";

    private SharedPreferences defaultSharedPreferences;

    //实现一个INBinder接口
    public final IBinder myBinder = new LocalBinder();

    //将该接口实现为一个继承Binder得内部类的对象！
    public class LocalBinder extends Binder {
        //在内部类实现一个返回对象的方法
        public AppListService getService() {
            return AppListService.this;
        }
    }

    /**
     * 绑定时执行
     */
    public void onCreate() {
        super.onCreate();
        log.info("onCreate方法执行开始");
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        log.info("服务开始绑定中");
        return myBinder;
    }

    @Override
    public boolean onUnbind(Intent intent) {
        log.info("服务取消绑定了");
        return false;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        log.info("服务开始执行开机启动");
        bootStart();
        return super.onStartCommand(intent, flags, startId);
    }


    /**
     * 开机启动
     */
    private void bootStart() {

        defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean hdpStartSwitch = defaultSharedPreferences.getBoolean("hdpStartSwitch", false);
        if (hdpStartSwitch) {
            startHdp();
        } else {
            log.info("startCCTV11: hdp直播没有设置开机启动");
            Log.i(TAG, "startCCTV11: hdp直播没有设置开机启动");
        }

        boolean xiguaStartSwitch = defaultSharedPreferences.getBoolean("xiguaStartSwitch", false);
        if (xiguaStartSwitch) {
            try {
                Intent intent = new Intent();
                ComponentName component = new ComponentName("com.ixigua.android.tv.wasu", "com.ixigua.android.tv.module.home.view.MainActivity");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                intent.setComponent(component);
                startActivity(intent);
            } catch (Exception e) {
                log.error("没有安装鲜时光{}", e);
                android.widget.Toast.makeText(getApplicationContext(), "没有安装鲜时光", Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.i(TAG, "start: 鲜时光没有设置开机启动");
            log.info("start: 鲜时光没有设置开机启动");
        }

        boolean biliTVStartSwitch = defaultSharedPreferences.getBoolean("biliTVStartSwitch", false);
        if (biliTVStartSwitch) {
            try {
                Intent insIntent = new Intent();
                insIntent.setClassName("com.xiaodianshi.tv.yst", "com.xiaodianshi.tv.yst.ui.main.MainActivity");
                insIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(insIntent);
                log.info("哔哩哔哩startActivity完成");
            } catch (Exception e) {
                log.error("没有安装哔哩哔哩TV{}", e);
                android.widget.Toast.makeText(getApplicationContext(), "没有安装哔哩哔哩TV", Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.i(TAG, "start: Bili没有设置开机启动");
            log.info("start: Bili没有设置开机启动");
        }

        boolean huoxingStartSwitch = defaultSharedPreferences.getBoolean("huoxingStartSwitch", false);
        if (huoxingStartSwitch) {
            try {
                Intent insIntent = new Intent();
                insIntent.setClassName("com.xiaojie.tv", "com.tv.core.main.LiveActivity");
                insIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(insIntent);
            } catch (Exception e) {
                log.error("没有安装火星视频{}", e);
                android.widget.Toast.makeText(getApplicationContext(), "没有安装火星视频", Toast.LENGTH_SHORT).show();
            }
        } else {
            Log.i(TAG, "start: 火星直播没有设置开机启动");
            log.info("start: 火星直播没有设置开机启动");
        }

    }

    /**
     * 打开hdp直播的指定频道
     */
    private void startHdp() {
        try {
            String channelString = defaultSharedPreferences.getString("channel", "1");
            Integer channel = 1;
            try {
                channel = Integer.parseInt(channelString);
            } catch (Exception e) {
                log.error("频道设置错误，应该是数字", e);
                android.widget.Toast.makeText(getApplicationContext(), "频道设置错误，应该是数字", Toast.LENGTH_SHORT).show();
            }

            Intent insIntent = new Intent();
            insIntent.setClassName("hdpfans.com", "hdp.player.StartActivity");
            insIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // 实时打开
            insIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            /** 隐藏启动图片 **/
//            insIntent.putExtra("HIDE_LOADING_DEFAULT", true);
            insIntent.putExtra("HIDE_EXIT_DIAG", true);

            boolean hdpStartChannelSwitch = defaultSharedPreferences.getBoolean("hdpStartChannelSwitch", false);
            if (hdpStartChannelSwitch) {
                insIntent.putExtra("ChannelNum", channel);
            }

            startActivity(insIntent);
            log.info("hdp服务开始执行开机启动");
        } catch (Exception e) {
            log.error("没有安装HDP直播{}", e);
            android.widget.Toast.makeText(getApplicationContext(), "没有安装HDP直播", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
